package com.yswee.mallnavi.activity.base;

import com.yswee.framework.R;
import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.widget.TitleBar;
import com.yswee.framework.widget.base.WebView.MWebChromeClient;
import com.yswee.framework.widget.loading.LoadingWebView;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class WebViewActivity extends BaseActivity {

	public static final String INTENT_URL = "url";
	public static final String INTENT_TITLE = "title";
	
	private String mUrl;
	private String mTitle;
	
	protected TitleBar mTitleBar;
	protected LoadingWebView mWebview;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
	}
	
	@Override
	protected void onQueryArguments(Intent intent) {
		super.onQueryArguments(intent);
		mUrl = intent.getStringExtra(INTENT_URL);
		mTitle = intent.getStringExtra(INTENT_TITLE);
	}
	
	@Override
	protected void onFindView() {
		super.onFindView();
		mTitleBar = (TitleBar) findViewById(R.id.titlebar);
		mWebview = (LoadingWebView) findViewById(R.id.uvwebview);
	}
	
	@Override
	protected void onBindListener() {
		super.onBindListener();
		mTitleBar.setLeftTitleOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mWebview.getWebView().setWebChromeClient(
				new MWebChromeClient(mWebview.getWebView(), WebViewActivity.this) {
					@Override
					public void onReceivedTitle(android.webkit.WebView view, String title) {
						super.onReceivedTitle(view, title);
						if (StringUtils.isEmpty(mTitle)) {
							mTitle = title;
							mTitleBar.setTitle(title);
						}
					}
				});
	}
	
	@Override
	protected void onApplyData() {
		super.onApplyData();
		mTitleBar.setTitle(mTitle);
		if (!StringUtils.isEmpty(mUrl)) {
			mWebview.loadUrl(mUrl);
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(mWebview.onKeyDown(keyCode, event)) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mWebview != null) {
			mWebview.destroy();
		}
	}
}
